package com.annoncesautomobile.vue.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.util.ModelXML;

public class ModalMakeCallPhone {

	private static final String TYPE_TEL_ANNONCE = "typeTelAnnonce";
	private static final String TYPE_TEL_CLIENT = "typeTelClient";

	public static void makeCallPhone(Activity activity, String typeTel, Long id, String phoneNumber) {

		final Long _id = id;
		final String _phoneNumber = phoneNumber;
		final String _typeTel = typeTel;
		final Activity _activity = activity;
		final Dialog dialog = new Dialog(_activity);

		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.modal_go_nogo);
		final TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText("Appeler le "+_phoneNumber+" ?");
		//text.setText("Appeler ?");

		Button buttonNogo = (Button) dialog.findViewById(R.id.buttonNogo);
		buttonNogo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		Button buttonGo = (Button) dialog.findViewById(R.id.buttonGo);
		buttonGo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				text.setText("Appel du "+_phoneNumber+" en cours...");
				if(_typeTel != null) {
					if(_typeTel.equals(TYPE_TEL_ANNONCE)) {
						new PostAnnonceStatsTel().execute(Long.valueOf(_id).intValue());
					}
					if(_typeTel.equals(TYPE_TEL_CLIENT)) {
						// Pas de stats sur un phoneCall detailClientPro
					}
				}

				Intent callIntent = new Intent(Intent.ACTION_CALL);
				callIntent.setData(Uri.parse("tel:"+_phoneNumber));
				_activity.startActivityForResult(callIntent, 0);
				dialog.dismiss();
			}
		});

		dialog.show();
	}


	public static class PostAnnonceStatsTel extends AsyncTask<Integer, Void, Void> {

		@Override
		protected Void doInBackground(Integer... params) {
			System.out.println("Tel idAnnonce : "+params[0]);
			ModelXML.getInstance().postAnnonceStatsTel(params[0]);
			return null;
		}
	}
}
